package com.ubb.dbms.core.fileManagers.dataFileManager;

import java.util.Iterator;
import java.util.Map;

import com.ubb.dbms.domain.queryResult.SelectedRow;
import com.ubb.dbms.domain.xml.Table;

public class DataFileParser implements Iterator<SelectedRow> {

	private DataFileIO dataFileIO;
	private Table table;
	private Map<String, String> projections;
	private int rowNrCounter = 0;
	private SelectedRow nextRow;
	
	public DataFileParser(Table table, String usesDatabase, Map<String, String> projections) {
		this.table = table;
		this.projections = projections;
		
		String tableFileFolder = DataFileManagerImpl.getTableFileFolder(usesDatabase, table.getTableName());
		String tableFilePath = DataFileManagerImpl.getTableFilePath(tableFileFolder, table);
		String rsTableFilePath = DataFileManagerImpl.getRsTableFilePath(tableFileFolder, table);
		
		int rowLength = Integer.parseInt(table.getRowLenght());

		dataFileIO = new DataFileIO(tableFilePath, rsTableFilePath, 0, rowLength);
	}

	@Override
	public boolean hasNext() {
		rowNrCounter = dataFileIO.getNextNotDeletedRowNr(rowNrCounter);
		nextRow = dataFileIO.readRow(table, rowNrCounter, projections);
		return nextRow != null;
	}

	@Override
	public SelectedRow next() {
		return nextRow;
	}
	
	public int getRowNrCounter() {
		return rowNrCounter;
	}
	
	@Override
	public void remove() {
		throw new UnsupportedOperationException("remove method not implemented on DataFileParser");
	}

}
